#!/bin/sh

# 4个主机
host="st01-game-dubi00.st01.baidu.com cq01-game-dubi00.cq01.baidu.com st01-game-dubi01.st01.baidu.com cq01-game-dubi01.cq01.baidu.com"
tomcat_path="/home/work/tomcat_8080_dqweb/logs/monitor /home/work/tomcat_8081_dqweb/logs/monitor"

if [[ $# -eq 0 ]]
then
	yyyyMMdd=$(date +%Y-%m-%d)
	HH=$(date +"%H" "-d -1 hours")
	now=1
elif [[ $# -eq 1 ]]
then
	yyyyMMdd=$(date -d "$1" +"%Y-%m-%d")
	HH=""
	now=2
elif [[ $# -eq 2 ]]
then
	yyyyMMdd=$(date -d "$1" +"%Y-%m-%d")
	HH=$2
	now=3
else
	echo "args is 0 or one is date or two is date hour"
fi
today=$(date +"%Y%m%d")
local_path="/home/hadoop/QianYi/cashier_log"
mkdir -p $local_path
cd $local_path

for h in ${host[@]}
do
	for t in ${tomcat_path}
	do
		# tomcat_8080_dqweb or tomcat_8081_dqweb
		tt=$(echo $t | awk -F"work/|/logs" '{print $2}')
		cashier=${h}_${tt}_"cashier.log."$yyyyMMdd
		{
			if [[ $now -eq 1 ]];then
				$(wget -c -t 0 --limit-rate 15M ftp://${h}${t}/cashier.log -O $cashier)
			elif [[ $now -eq 2 || $now -eq 3 ]];then
				if [[ $1==$today ]];then
					$(wget -c -t 0 --limit-rate 15M ftp://${h}${t}/cashier.log -O $cashier)
				else
					$(wget -c -t 0 --limit-rate 15M ftp://${h}${t}/cashier.log.${yyyyMMdd} -O $cashier)
				fi
			fi
		} &
	done
done
wait
## 过滤当前小时的日志
cashier_path="cashier_log_$yyyyMMdd-$HH"
cashier_log=$(cat *$yyyyMMdd | grep -E "^$yyyyMMdd $HH:" > $cashier_path)
source /home/hadoop/.bashrc
yyyyMMdd=$(date -d "$yyyyMMdd" +"%Y%m%d")
$(hdfs dfs -put $cashier_path /flume/events/dianquan/nginx-collector/${yyyyMMdd}/${HH}00/)
